package cn.edu.scnu.semart.order.mapper;

import cn.edu.scnu.semart.model.order.Order;
import cn.edu.scnu.semart.model.order.OrderItem;
import cn.edu.scnu.semart.vo.order.OrderQueryVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
@Mapper
public interface OrderMapper extends BaseMapper<Order>{
    int insertOrder(Order order);
    int insertOrderItems(@Param("orderId") Integer orderId, @Param("items") List<OrderItem> items);
    Order selectOrderById(Integer orderId);
    List<Order> selectOrdersByUserId(@Param("userId") Integer userId);
    void updateOrderStatus(@Param("orderId") Integer orderId, @Param("status") String status);
    int updateOrderPaymentStatus(@Param("orderNumber") String orderNumber, @Param("status") String status);
    List<OrderItem> selectOrderItems(Integer orderId);
    List<Map<String, Object>> getDailySales(@Param("start") Date start, @Param("end") Date end);
    List<Map<String, Object>> getRegionSales(@Param("start") Date start, @Param("end") Date end);
    String getOrderStatus(@Param("orderId") Integer orderId);
    Integer getOrderIdByNumber(@Param("orderNumber") String orderNumber);
    int completeOrder(@Param("orderId") Integer orderId);
    List<Order> findOrdersForAutoConfirm(Date confirmDate);
    List<OrderItem> selectOrderItemsByOrderIds(@Param("orderIds") List<Integer> orderIds);
    List<Order> selectOrdersForAutoConfirm(Date date);
    int updateCompleteTime(@Param("orderId") Integer orderId, @Param("completeTime") Date completeTime);
    List<OrderItem> selectOrderItemsByOrderNumber(String orderNumber);
    List<Order> selectOrdersByProductNameLike(@Param("productName") String productName);

    /**
     * 条件分页查询订单
     * @param page 分页参数
     * @param orderQueryVo 查询条件
     * @return 分页结果
     */
    IPage<Order> selectPage(Page<Order> page, @Param("vo") OrderQueryVo orderQueryVo);

    List<Order> selectOrdersByUserIdAndStatus(@Param("userId") Integer userId,
                                              @Param("status") String status);

    Integer selectIdByOrderNumber(@Param("orderNumber") String orderNumber);

    @Select("SELECT * FROM `order` WHERE order_number = #{outTradeNo} LIMIT 1")
    Order selectByOutTradeNo(@Param("outTradeNo") String outTradeNo);

    @Select("SELECT * FROM `order` WHERE pay_trade_no = #{payTradeNo} LIMIT 1")
    Order selectByPayTradeNo(@Param("payTradeNo") String payTradeNo);

    @Update("UPDATE `order` SET payment_time = #{paymentTime} WHERE order_id = #{orderId}")
    int updatePaymentTime(@Param("orderId") Integer orderId, @Param("paymentTime") Date paymentTime);
}